import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoSet {

    public static void test1 (Jedis jedis) {
        System.out.println("sadd  smembers");
        jedis.flushDB();

        jedis.sadd("key","111","222","333");

        Set<String> members = jedis.smembers("key");
        System.out.println("members = " + members);
    }

    public static void test2 (Jedis jedis) {
        System.out.println("sismember");
        jedis.flushDB();

        jedis.sadd("key","111","222","333");
        System.out.println(jedis.sismember("key", "111"));
    }

    public static void test3 (Jedis jedis) {
        System.out.println("scard");
        jedis.flushDB();

        jedis.sadd("key","111","222","333");
        long result = jedis.scard("key");
        System.out.println("result = " + result);
    }

    public static void test4(Jedis jedis) {
        System.out.println("spop");
        jedis.flushDB();

        jedis.sadd("key","111","222","333");

        Set<String> members = jedis.smembers("key");
        System.out.println("members = " + members);

        String pop = jedis.spop("key");
        System.out.println("pop = " + pop);

        members = jedis.smembers("key");
        System.out.println("members = " + members);

    }

    public static void test5(Jedis jedis) {
        System.out.println("sinter");
        jedis.flushDB();

        jedis.sadd("key","111","222","333");
        jedis.sadd("key2","111","333","555");

        Set<String> result = jedis.sinter("key", "key2");
        System.out.println("result = " + result);
    }

    public static void test6(Jedis jedis) {
        System.out.println("sinterstore");
        jedis.flushDB();
        jedis.sadd("key","111","222","333");
        jedis.sadd("key2","111","333","555");

        jedis.sinterstore("key3", "key", "key2");

        Set<String> key3 = jedis.smembers("key3");
        System.out.println("key3 = " + key3);

    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4 (jedis);
            test5(jedis);
        }
    }
}
